package com.stupidzhe.vbreview.service.impl;

import com.stupidzhe.vbreview.bean.ResultBean;
import com.stupidzhe.vbreview.cache.StudentCache;
import com.stupidzhe.vbreview.dao.StudentMapper;
import com.stupidzhe.vbreview.domain.Student;
import com.stupidzhe.vbreview.domain.Teacher;
import com.stupidzhe.vbreview.service.StudentService;
import com.stupidzhe.vbreview.util.Md5Util;
import com.stupidzhe.vbreview.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Time;
import java.util.regex.Pattern;

@Service
public class StudentServiceImpl implements StudentService {

    private final StudentCache studentCache;

    private final StudentMapper studentMapper;

    private final
    Pattern pattern = Pattern.compile("^[0-9A-Za-z][.-_0-9A-Za-z]*@[0-9A-Za-z]+(\\.[0-9A-Za-z]+)+$");

    public StudentServiceImpl(StudentCache studentCache, StudentMapper studentMapper) {
        this.studentCache = studentCache;
        this.studentMapper = studentMapper;
    }

    @Override
    public String login(String account, String password, HttpServletRequest request) {
        String md5Password = password + Md5Util.salt;
        md5Password = Md5Util.getPwd(md5Password);
        Student student = studentMapper.selectByAccount(account);
        if (null == student || !student.getStudentPwd().equals(md5Password) || "0".equals(
                student.getStatus())) {
            return "";
        }
        try {
            studentMapper.updateByPrimaryKey(student);
            String md5Account = Md5Util.getPwd(student.getStudentNum() + Md5Util.salt);
            studentCache.addStudent(account, student);
            return md5Account;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * teacherId没加
     */
    @Override
    public ResultBean register(String number, String name, String pwd, Integer teacherId, Integer classId) {
        String md5Password = pwd + Md5Util.salt;
        md5Password = Md5Util.getPwd(md5Password);
        Student student = new Student();
        student.setStudentNum(number);
        student.setStudentName(name);
        student.setStudentPwd(md5Password);
        student.setStatus("1");
        student.setCreateTime(TimeUtil.getCurrentTime());
        student.setClassId(classId);
        try {
            studentMapper.insert(student);
        } catch (Exception e) {
            return new ResultBean(false, "注册失败:学号重复");
        }
        return new ResultBean(true, "注册成功");
    }

    @Override
    public ResultBean edit(Student student, String email) {
        if (!pattern.matcher(email).matches()) {
            return new ResultBean(false, "邮件格式出错");
        }
        try {
            student.setEmail(email);
            studentMapper.updateByPrimaryKeySelective(student);
        } catch (Exception e) {
            return new ResultBean(false, "更新失败");
        }
        studentCache.addStudent(student.getStudentNum(), student);
        return new ResultBean(true, "邮箱更新成功");
    }

    @Override
    public boolean validate(String number) {
        Student student = studentMapper.selectByAccount(number);
        return student != null;
    }

    @Override
    public Student selectById(Integer studentId) {
        return studentMapper.selectByPrimaryKey(studentId);
    }
}
